/**
 * 公司项目大部分是子系统单点模式，所有系统同域名
 * 所以缓存避免使用localStrong和cookie带来的负面影响，推荐使用sessionStrong
 */

const sysName = window.location.pathname.replace(/\//g, '') || 'admin';
const tokenKey = sysName + '_token';
const infoKey = sysName + '_info';
const permissionKey = sysName + '_permission';

export function getToken() {
  return sessionStorage.getItem(tokenKey);
}

export function setToken(token) {
  sessionStorage.setItem(tokenKey, token);
}

export function removeToken() {
  sessionStorage.removeItem(tokenKey);
}

export function setUserInfo(value) {
  sessionStorage.setItem(infoKey, JSON.stringify(value));
}

export function getUserInfo() {
  const userInfo = sessionStorage.getItem(infoKey);
  return userInfo ? JSON.parse(userInfo) : {};
}

export function removeUserInfo() {
  sessionStorage.removeItem(infoKey);
}

export function setUserPermission(arr) {
  sessionStorage.setItem(permissionKey, JSON.stringify(arr));
}

export function removeUserPermission() {
  sessionStorage.removeItem(permissionKey);
}

export function getUserPermission() {
  const userRight = sessionStorage.getItem(permissionKey);
  return userRight ? JSON.parse(userRight) : [];
}

export function removeAll() {
  sessionStorage.clear();
}
